package com.tg.app.activity.device.settings;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.tange.module.device.feature.DeviceFeatureObjectBox;
import com.tg.app.R;
import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.adapter.SettingAdapter;
import com.tange.module.device.feature.DeviceFeature;
import com.tg.app.helper.DeviceFeatureHelper;
import com.tg.data.bean.DeviceSettingsInfo;
import com.appbase.custom.base.SettingData;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.app.camera.Camera;
import com.tange.module.camera.hub.CameraHub;
import com.tg.data.media.OnICameraListener;
import com.tg.appcommon.android.Packet;
import com.appbase.custom.constant.PIRConstants;
import com.tg.appcommon.android.TGAlertDialog;

import java.util.ArrayList;

import static com.tg.app.activity.device.DeviceSettingsActivity.EXT_DEVICE_INFO;

public class PIRSensitivityActivity extends DeviceSettingsBaseActivity implements OnICameraListener {
    private ArrayList<SettingData> mModeLists = new ArrayList<>();
    private ArrayList<SettingData> mFilterLists = new ArrayList<>();
    private Camera camera;
    private DeviceSettingsInfo info;
    private int mSensitivity, mFilterOn;
    private SettingAdapter mModeAdapter, mFilterAdapter;
    private ListView mModeListView, mFiterListView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        hideActionBar();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pir_sensitivity);
        info = getIntent().getParcelableExtra(EXT_DEVICE_INFO);
        if (info != null && !TextUtils.isEmpty(info.uuid)) {
            camera = CameraHub.getInstance().getCamera(info.uuid);
            if (camera != null) {
                camera.registerICameraListener(this);
            }
        }

        initView();
        modifyToolBar();
        initData();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void initView() {
        backClickEvent();
        mModeAdapter = new SettingAdapter(mModeLists, this);
        mFilterAdapter = new SettingAdapter(mFilterLists, this);
        mModeListView = findViewById(R.id.settings_list_mode);
        mFiterListView = findViewById(R.id.settings_list_filter);
        mModeListView.setAdapter(mModeAdapter);
        mFiterListView.setAdapter(mFilterAdapter);
        mModeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    setPIRSensitivityCmd(PIRConstants.HIGH);
                } else if (position == 1) {
                    setPIRSensitivityCmd(PIRConstants.MEDIUM);
                } else if (position == 2) {
                    setPIRSensitivityCmd(PIRConstants.LOW);
                } else if (position == 3) {
                    setPIRSensitivityCmd(PIRConstants.OFF);
                }

            }
        });
        mFiterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    setPIRSensitivityFilterCmd(1);
                } else {
                    setPIRSensitivityFilterCmd(0);
                }
            }
        });
        findViewById(R.id.tv_pir_sensitivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTGAlertDialog(getString(R.string.detection_sensitivity), getString(R.string.text_dlaglog_pir_sensitivity));
            }
        });


        findViewById(R.id.tv_pir_sensitivity_filter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTGAlertDialog(getString(R.string.text_pir_sensitivity_filter), getString(R.string.text_dlaglog_pir_sensitivity_filter));
            }
        });

       if (info!=null){
           DeviceFeature deviceFeature = DeviceFeatureObjectBox.queryDeviceFeature(info.uuid);
           if (!DeviceFeatureHelper.supportAiSwitch(deviceFeature)){
               findViewById(R.id.rl_video_limpid).setVisibility(View.GONE);
           }
           if (!DeviceFeatureHelper.supportPIR(deviceFeature)){
               findViewById(R.id.rl_video_times).setVisibility(View.GONE);
           }
       }
    }

    private void showTGAlertDialog(String title, String content) {
        new TGAlertDialog(this).builder().setTitle(title).setMessage(content).setPositiveButton(R.string.sure, null).show();

    }

    private void initData() {
        // int sens;    //< 0:关闭; 1:低; 2:中; 3:高
        SettingData high = new SettingData(getString(R.string.settings_pir_sensitivity_high), 0);
        SettingData medium = new SettingData(getString(R.string.settings_pir_sensitivity_medium), 0);
        SettingData low = new SettingData(getString(R.string.settings_pir_sensitivity_low), 0);
        SettingData off = new SettingData(getString(R.string.settings_pir_sensitivity_off), 0);


        SettingData filterOn = new SettingData(getString(R.string.settings_pir_sensitivity_filter_on), 0);
        SettingData filterOff = new SettingData(getString(R.string.settings_pir_sensitivity_filter_off), 0);


        if (info != null) {
            if (info.pir == PIRConstants.OFF) {
                off.setFlag(1);
            } else if (info.pir == PIRConstants.LOW) {
                low.setFlag(1);
            } else if (info.pir == PIRConstants.MEDIUM) {
                medium.setFlag(1);
            } else if (info.pir == PIRConstants.HIGH) {
                high.setFlag(1);
            }
            mSensitivity = info.pir;
            mFilterOn = info.aiOn;
            if (info.aiOn == 0) {
                filterOff.setFlag(1);
            } else {
                filterOn.setFlag(1);
            }
        }
        mModeLists.add(high);
        mModeLists.add(medium);
        mModeLists.add(low);
        mModeLists.add(off);
        mModeAdapter.notifyDataSetChanged();

        mFilterLists.add(filterOn);
        mFilterLists.add(filterOff);
        mFilterAdapter.notifyDataSetChanged();

    }

    @Override
    protected void onSetupFailed() {

    }

    private void setPIRSensitivityFilterCmd(int type) {
        if (camera != null) {
            mFilterOn = type;
            startSendui();
            camera.sendIOCtrl(AVIOCTRLDEFs.TCI_CMD_SET_AI_REQ, AVIOCTRLDEFs.Tcis_AiStatus.parseContent(0x00000001, type));
        }
    }

    private void setPIRSensitivityCmd(int type) {
        if (camera != null) {
            mSensitivity = type;
            startSendui();
            camera.sendIOCtrl(AVIOCTRLDEFs.TCI_CMD_SET_PIR_REQ, AVIOCTRLDEFs.Tcis_PirSens.parseContent(type));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (camera != null) {
            camera.registerICameraListener(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (camera != null) {
            camera.unregisterICameraListener(this);
        }
    }

    private void setFlag(ArrayList<SettingData> datas, int position) {
        for (int i = 0; i < datas.size(); i++) {
            if (i == position) {
                datas.get(i).setFlag(1);
            } else {
                datas.get(i).setFlag(0);
            }
        }

    }

    @Override
    public void receiveUpdateConnectStates(int state) {

    }

    @Override
    public void receiveIOCtrlData(int type, byte[] data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {


                if (type == AVIOCTRLDEFs.TCI_CMD_SET_COMMAND_RESP) {
                    int command = Packet.byteArrayToInt_Little(data, 0);
                    int result = Packet.byteArrayToInt_Little(data, 4);
                    completeSend();
                    if (command == AVIOCTRLDEFs.TCI_CMD_SET_PIR_REQ) {
                        if (result == 0) {
                            info.pir = mSensitivity;
                            if (mSensitivity == PIRConstants.HIGH) {
                                setFlag(mModeLists, 0);
                            } else if (mSensitivity == PIRConstants.MEDIUM) {
                                setFlag(mModeLists, 1);
                            } else if (mSensitivity == PIRConstants.LOW) {
                                setFlag(mModeLists, 2);
                            } else if (mSensitivity == PIRConstants.OFF) {
                                setFlag(mModeLists, 3);
                            }
                            mModeAdapter.notifyDataSetChanged();
                            sendUpdateSettingBroadcast(info);
                        }
                    } else if (command == AVIOCTRLDEFs.TCI_CMD_SET_AI_REQ) {
                        if (result == 0) {
                            info.aiOn = mFilterOn;
                            if (mFilterOn == 1) {
                                setFlag(mFilterLists, 0);
                            } else {
                                setFlag(mFilterLists, 1);
                            }
                            mFilterAdapter.notifyDataSetChanged();
                            sendUpdateSettingBroadcast(info);
                        }
                    }

                }
            }
        });
    }
}
